package com.deng.api.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.deng.api.entity.SysRole;
import com.deng.api.entity.SysRoleMenu;
import com.deng.api.entity.SysUserRole;
import com.deng.api.mapper.SysRoleMapper;
import com.deng.api.mapper.SysRoleMenuMapper;
import com.deng.api.mapper.SysUserRoleMapper;
import com.deng.api.service.ISysRoleService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.deng.api.vo.MenuParamsVO;
import com.deng.api.vo.PageVO;
import com.deng.commons.common.BaseResult;
import com.deng.commons.common.ErrorCode;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * <p>
 * 角色表 服务实现类
 * </p>
 *
 * @author deng
 * @since 2022-08-10
 */
@Service
public class SysRoleServiceImpl extends ServiceImpl<SysRoleMapper, SysRole> implements ISysRoleService {
    @Resource
    private SysRoleMapper sysRoleMapper;
    @Resource
    private SysUserRoleMapper sysUserRoleMapper;
    @Resource
    private SysRoleMenuMapper sysRoleMenuMapper;

    @Override
    public List<SysRole> getRolesByUid(Long uid) {
        return sysRoleMapper.getRolesByUid(uid);
    }

    @Override
    public List<SysRole> getRolesByMid(Long mid) {
        return sysRoleMapper.getRolesByMid(mid);
    }

    @Override
    public BaseResult getRoles() {
        List<SysRole> sysRoles = sysRoleMapper.selectList(null);
        return BaseResult.success(sysRoles);
    }

    @Override
    public BaseResult getPages(Integer page, Integer size, SysRole role) {
        Page<SysRole> p = new Page<>(page,size);
        //根据角色名查找
        LambdaQueryWrapper<SysRole> wrapper = new LambdaQueryWrapper<>();
        if (role != null && !StrUtil.isEmptyIfStr(role.getName())){
            wrapper.eq(SysRole::getName,role.getName());
        }
        Page<SysRole> rolePage = sysRoleMapper.selectPage(p, wrapper);
        return BaseResult.success(new PageVO<>(rolePage.getRecords(),rolePage.getTotal()));
    }

    @Override
    public BaseResult addRole(SysRole sysRole) {
        return BaseResult.result(sysRoleMapper.insert(sysRole)>0);
    }

    @Override
    @Transactional
    public BaseResult delRole(Set<Long> rids) {
        //查看是否有用户拥有该角色，如果有就不能删除
        LambdaQueryWrapper<SysUserRole> wrapper = new LambdaQueryWrapper<>();
        wrapper.in(SysUserRole::getRoleId,rids);
        List<SysUserRole> userRoles = sysUserRoleMapper.selectList(wrapper);
        if (userRoles != null && userRoles.size()>0){
            return BaseResult.fail(ErrorCode.CODE_1008);
        }
        //角色跟menu也有关联
        LambdaQueryWrapper<SysRoleMenu> wrapper2 = new LambdaQueryWrapper<>();
        wrapper2.in(SysRoleMenu::getRoleId,rids);
        sysRoleMenuMapper.delete(wrapper2);
        //删除角色
        sysRoleMapper.deleteBatchIds(rids);
        return BaseResult.success();
    }

    @Override
    public BaseResult menu(MenuParamsVO menuParamsVO) {
        //需要获取当前角色的所有权限菜单
        LambdaQueryWrapper<SysRoleMenu> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(SysRoleMenu::getRoleId,menuParamsVO.getId());
        List<SysRoleMenu> rolesMenus = sysRoleMenuMapper.selectList(wrapper);
        List<Long> list = rolesMenus.stream().map(rm -> rm.getMenuId()).collect(Collectors.toList());
        /*
            如果已经存在的权限菜单就不操作，如果不存在的就添加到sys_roles_menus表中,
            此处就是menuParamsVO.getMenus()和list对比，没有就插入
         */
        menuParamsVO.getMenus().forEach(m->{
            if (!list.contains(m.getId())){
                sysRoleMenuMapper.insert(new SysRoleMenu(m.getId(),menuParamsVO.getId()));
            }
        });
        return BaseResult.success();
    }

}
